function addReplyEle(parentCommentId) {
    let template = document.createElement("div");
    template.className = "addReply";
    template.innerHTML = `<div contenteditable="true"></div>
                    <span class="btn" onclick="replyMsg(this,'${parentCommentId}')">回复</span>`;
    return template;
}

function addComment(ele, parentCommentId) {
    let nextElement = ele.parentElement.parentElement.parentElement.nextElementSibling;
    if (nextElement) {
        if (nextElement.className === "addReply")
            nextElement.parentElement.removeChild(nextElement);
        else
            nextElement.parentElement.insertBefore(addReplyEle(parentCommentId), nextElement);
    } else {
        ele.parentElement.parentElement.parentElement.parentElement.appendChild(addReplyEle(parentCommentId));
    }
}

function dateFormat(milliseconds, separator) {
    if (milliseconds === "")
        return "";
    let date = new Date(milliseconds);
    let year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate();
    if (month < 10)
        month = "0" + month;
    if (day < 10)
        day = "0" + day;
    return year + separator + month + separator + day;
}

function showReplyEle(obj, count) {
    let template = document.createElement("div");
    template.className = "reply";
    let sub = document.createElement("div");
    sub.className = "sub";
    let content = ` <p class="customerName">
                        <a href="">${obj.customer.customerName} : </a>
                        <span>${obj.dynamicComment.customerCommentContent}</span>
                    </p>
                    <p class="spanBox">
                        <span>${dateFormat(obj.dynamicComment.customerCommentTime, "-")}</span>`;
    if (count < 2) {
        content += `    <span class="answer">`;
        if (obj.comment > 0)
            content += `    <span data-type="off" data-count="${count}" onclick="showComment(this,'${obj.dynamicComment.dynamicCommentId}')">查看所有回复</span>`;
        content += `        <span onclick="addComment(this,'${obj.dynamicComment.dynamicCommentId}')">回复 ${obj.comment}</span>`;
        content += `    </span>`
    }
    content += `</p>`;
    sub.innerHTML = content;
    template.appendChild(sub);
    return template;
}

function showComment(ele, parentCommentId) {
    let ce = ele.parentElement.parentElement.parentElement;
    if (ele.getAttribute("data-type") === "on") {
        ele.setAttribute("data-type", "off");
        ele.innerText = "查看所有回复";
        while (ce.nextElementSibling) {
            ce.parentElement.removeChild(ce.nextElementSibling);
        }
    } else {
        // ajax请求 通过 parentCommentId 查询
        let url = ROOTSources + "/dynamicComment/getByFatherId?fatherId=" + parentCommentId;
        let ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET", url, true);
        ajax.send();
        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                    let result = JSON.parse(ajax.responseText);
                    console.log(result);
                    if (result.status === 1) {
                        ele.setAttribute("data-type", "on");
                        ele.innerText = "收起所有回复";
                        let a = parseInt(ele.getAttribute("data-count")) + 1;
                        for (let obj of result.data) {
                            ce.parentElement.appendChild(showReplyEle(obj, a));
                        }

                    }
                }
            }
        };
    }
}


// 提交事件
function replyMsg(ele, parentCommentId) {
    let content = ele.previousElementSibling.innerText
        .replace(/\n/g, "<br/>")
        .replace(/ /g, "&nbsp;");
    // ajax请求
    let sendContent = {
        "customerId": customerId,
        "dynamicId": dynamicId,
        "fatherCommentId": parentCommentId,
        "customerCommentContent": content
    };
    let url = ROOTSources + "/dynamicComment/add";
    let ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
    ajax.open("POST", url, true);
    ajax.setRequestHeader("Content-Type", "application/json");
    ajax.send(JSON.stringify(sendContent));
    ajax.onreadystatechange = function () {
        if (ajax.readyState === 4) {
            if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                window.history.go(0);
            }
        }
    }
}

function likeFunction(ele) {
    let inner = ele.innerText;
    let count = parseInt(inner.replace(/[^0-9]/ig, ""));
    let url = ROOTSources + "/dynamic/like/" + customerId + "/" + dynamicId;
    let ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
    ajax.open("GET", url, true);
    ajax.send();
    ajax.onreadystatechange = function () {
        if (ajax.readyState === 4) {
            if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                try {
                    let result = JSON.parse(ajax.responseText);
                    if (result.status === 1) {
                        let type = 1 * (result.data);
                        let output = count + type;
                        if (type > 0) {
                            ele.innerText = "已赞(" + output + ")";
                        } else {
                            ele.innerText = "赞(" + output + ")";
                        }
                    }
                } catch (e) {
                    window.location.href = ROOTSources;
                }
            }
        }
    }
}

function collectFunction(ele) {
    let inner = ele.innerText;
    let count = parseInt(inner.replace(/[^0-9]/ig, ""));
    let url = ROOTSources + "/dynamic/collect/" + customerId + "/" + dynamicId;
    let ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
    ajax.open("GET", url, true);
    ajax.send();
    ajax.onreadystatechange = function () {
        if (ajax.readyState === 4) {
            if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                try {
                    let result = JSON.parse(ajax.responseText);
                    if (result.status === 1) {
                        let type = 1 * (result.data);
                        let output = count + type;
                        if (type > 0) {
                            ele.innerText = "已收藏(" + output + ")";
                        } else {
                            ele.innerText = "收藏(" + output + ")";
                        }
                    }
                } catch (e) {
                    window.location.href = ROOTSources;
                }
            }
        }
    }
}